package com.easylinkin.linkappapi.hcmy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.hcmy.entity.LivestockFarmers;
import com.easylinkin.linkappapi.hcmy.entity.LivestockFarming;
import com.easylinkin.linkappapi.hcmy.entity.LivestockStatistic;
import com.easylinkin.linkappapi.hcmy.service.LivestockService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * class info :
 *
 * @author liuqihang
 * @date 2021/10/15 17:40
 */
@RestController
@RequestMapping("/hcmy/livestock")
public class LivestockController {

    @Resource
    private LivestockService livestockService;

    /* 面源畜禽养殖  */

    @PostMapping("saveFarming")
    public RestMessage saveFarming(@RequestBody @Valid LivestockFarming livestockFarming) {
        Assert.notNull(livestockFarming, "畜禽养殖不能为空");
        String result = livestockService.saveLivestockFarmingData(livestockFarming);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("updateFarming")
    public RestMessage updateFarming(@RequestBody @Valid LivestockFarming livestockFarming) {
        Assert.notNull(livestockFarming, "畜禽养殖不能为空");
        String result = livestockService.updateLivestockFarmingData(livestockFarming);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("getFarmingPage")
    public RestMessage getFarmingPage(@RequestBody RequestModel<LivestockFarming> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        IPage<LivestockFarming> livestockFarmersList = livestockService.getLivestockFarmingPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(livestockFarmersList).build();
    }

    @PostMapping("deleteFarming")
    public RestMessage deleteFarming(@RequestBody LivestockFarming livestockFarming) {
        Assert.notNull(livestockFarming, "畜禽养殖不能为空");
        String result = livestockService.deleteLivestockFarmingData(livestockFarming);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }




    /* 面源畜禽养殖户  */

    @PostMapping("saveFarmers")
    public RestMessage saveFarmers(@RequestBody @Valid LivestockFarmers livestockFarmers) {
        Assert.notNull(livestockFarmers, "畜禽养殖户不能为空");
        String result = livestockService.saveLivestockFarmersData(livestockFarmers);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("updateFarmers")
    public RestMessage updateFarmers(@RequestBody @Valid LivestockFarmers livestockFarmers) {
        Assert.notNull(livestockFarmers, "畜禽养殖户不能为空");
        String result = livestockService.updateLivestockFarmersData(livestockFarmers);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("getFarmersPage")
    public RestMessage getFarmersPage(@RequestBody RequestModel<LivestockFarmers> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        IPage<LivestockFarmers> livestockFarmersList = livestockService.getLivestockFarmersPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(livestockFarmersList).build();
    }

    @PostMapping("deleteFarmers")
    public RestMessage deleteFarmers(@RequestBody LivestockFarmers livestockFarmers) {
        Assert.notNull(livestockFarmers, "畜禽养殖户不能为空");
        String result = livestockService.deleteLivestockFarmersData(livestockFarmers);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    /**
     * 污染物排放
     * @param requestModel
     * @return
     */
    @PostMapping("getPollutantDischargePage")
    public RestMessage getLivestockFarmingPollutantDischargePage(@RequestBody RequestModel<LivestockFarming> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        IPage<LivestockStatistic> livestockFarmersList = livestockService.getLivestockFarmingPollutantDischargePage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(livestockFarmersList).build();
    }

    /**
     * 猪粪污水年产
     * @param requestModel
     * @return
     */
    @PostMapping("getPigManureSewagePage")
    public RestMessage getPigManureSewagePage(@RequestBody RequestModel<LivestockFarming> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        IPage<LivestockStatistic> livestockFarmersList = livestockService.getPigManureSewagePage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(livestockFarmersList).build();
    }
}
